package com.itheima.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/sesDemo2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SessionDemo2...");
        // 获取Session,如果不存在，就让容器构建一个session;如果存在，就不需要构建
        HttpSession httpSession = req.getSession();
        System.out.println("id:"+httpSession.getId());
        // 从Session取数据
        String userId = (String)httpSession.getAttribute("userId");
        String username = (String)httpSession.getAttribute("username");
        System.out.println("从session取userId:"+userId);
        System.out.println("从session取username:"+username);
    }
}
